//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Class implementing New-Junction command.
//
// Creation Date: Dec 13, 2006
//
//---------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;

using Microsoft.Win32.SafeHandles;

namespace Pscx.Commands.FileSystem.Ntfs
{
    [Cmdlet(VerbsCommon.New, PscxNouns.Junction)]
    [Description("Creates NTFS directory junctions.")]
    public partial class NewJunctionCommand : NewLinkCommandBase
    {
        protected override void ProcessRecord()
        {
            DirectoryInfo junction = new DirectoryInfo(FullPath);
            
            if(junction.Exists)
            {
                FileSystemInfo[] children = junction.GetFileSystemInfos();
                if(children != null && children.Length > 0)
                {
                    WriteError(PscxErrorRecord.DirectoryNotEmptyError(FullPath));
                    return;
                }
            }
            else
            {
                junction.Create();
            }

            if(!ReparsePointHelper.CreateJunction(FullPath, FullTarget))
                WriteError(PscxErrorRecord.LastWin32Error("CreateJunctionFailed", ErrorCategory.NotSpecified, FullPath));
            else
                WriteObject(new DirectoryInfo(FullPath));
        }
    }
}
